/* Copyright 2010 Big Blue Brains
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.bigbluebrains.android;

import java.util.Formatter;


public class ArabicGlyph {
	private String characters;
	private String isolatedForm;
	private String initialForm;
	private String finalForm;
	private String medialForm;

	public ArabicGlyph(String characters, String isolatedForm,
			String initialForm, String finalForm, String medialForm) {
		this.characters = characters;
		this.isolatedForm = isolatedForm;
		this.initialForm = initialForm;
		this.finalForm = finalForm;
		this.medialForm = medialForm;
	}

	public boolean isLigature() {
		return characters.length() > 1;
	}
	
	public boolean canConnectToPrevious() {
		return finalForm != null;
	}
	
	public boolean canConnectToNext() {
		return initialForm != null;
	}
	
	public boolean canConnectToBoth() {
		return medialForm != null;
	}

	public String getCharacters() {
		return characters;
	}

	public String getIsolatedForm() {
		return isolatedForm;
	}

	public String getInitialForm() {
		return initialForm;
	}

	public String getFinalForm() {
		return finalForm;
	}

	public String getMedialForm() {
		return medialForm;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((characters == null) ? 0 : characters.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArabicGlyph other = (ArabicGlyph) obj;
		if (characters == null) {
			if (other.characters != null)
				return false;
		} else if (!characters.equals(other.characters))
			return false;
		return true;
	}
	
	public static String dumpUnicode(String s) {
		StringBuilder sb = new StringBuilder();
		for(Character c : s.toCharArray()) {
			Formatter f = new Formatter();
			sb.append(f.format("\\u%04x",(int)c)).append("\n");
		}
		return sb.toString();
	}
	
	@Override
	public String toString() {
		return "ArabicGlyph [characters=" + dumpUnicode(characters) + ", isLigature()="
				+ isLigature() + ", canConnectToPrevious()="
				+ canConnectToPrevious() + ", canConnectToNext()="
				+ canConnectToNext() + ", canConnectToBoth()="
				+ canConnectToBoth() + "]";
	}
}
